var $ = require;

var path = $('path');
require.paths.unshift(path.join(__dirname, '../..', 'utils'));  //add services to require.paths

var vows = $('vows'),
    assert = $('assert'),
    _ = $('underscore'),
    util = $('dbWrapperUtil');

vows.describe('test dbWrapperUtil').addBatch({

    //one context, may contains many topic or nested context
    'dbWrapperUtil config': {
        topic: function(){
            this.callback(null, util.config);
        },

        'dbconfig is not null': function(err, config){
                assert.isNotNull(config, 'dbWrapperUtil must has config');
        },

        'dbconfig is the same with dbConfig': function(err, config){
             var dbconfig = require('../../config.js').dbconfig;
             assert.isTrue(_.isEqual(config, dbconfig), 'dbconfig not the same with dbconfig.js');
        }
    },

    'dbWrapperUtil dbtemplate method': {
        topic: function(){
            this.callback();
        },

        'dbwrapper is not null': function(){
            var dbwrapper = util.getDBWrapper();
            assert.isNotNull(dbwrapper, 'dbwrapper must not be null');
            assert.isFalse(dbwrapper.isConnected(), 'dbwrapper is not connected');
        },

        'dbwrapper fetchAll':{
            topic: function(){
                var me = this;
                util.dbtemplate(function(dbwrapper, closeCallback){
                    dbwrapper.fetchAll('select * from user', null, function(err, result){
                        me.callback(err, result);
                        closeCallback();
                    });
                });
            },

            'dbwrapper fetchAll check': function(err, result){
                assert.isNotNull(result);
                assert.isArray(result);
                assert.isTrue(result.length > 0);
            }
        }
    }
}).run();
